package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.AssignmentQuizTag;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.entities.UserAssignmentQuizTag;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.services.AssignmentQuizTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api")
public class UserAssignmentQuizTagController {
    @Autowired
    private AssignmentQuizTagService assignmentQuizTagService;

    @GetMapping("programs/{programId}/assignmentQuizTags")
    public ResponseEntity getAssignmentQuizTagsByProgramId(@PathVariable Long programId) {
        List<Map> list = assignmentQuizTagService.getAssignmentQuizTagsByProgramId(programId);

        return new ResponseEntity(list, HttpStatus.OK);
    }

    @GetMapping("assignments/{assignmentId}/quizzes/{quizId}/students/{studentId}/userAssignmentQuizTags")
    public ResponseEntity getStudentAssignmentQuizTags(@PathVariable Long assignmentId,
                                                       @PathVariable Long quizId,
                                                       @PathVariable Long studentId,
                                                       @Auth User current) {
            List<UserAssignmentQuizTag> userAssignmentQuizTagsInfo =
                assignmentQuizTagService.getStudentAssignmentQuizTags(assignmentId, quizId, studentId, current);

        return new ResponseEntity(userAssignmentQuizTagsInfo, HttpStatus.OK);
    }

    @PutMapping("assignments/{assignmentId}/quizzes/{quizId}/students/{studentId}/userAssignmentQuizTags")
    public ResponseEntity editStudentAssignmentQuizTags(@PathVariable Long assignmentId,
                                                        @PathVariable Long quizId,
                                                        @PathVariable Long studentId,
                                                        @Auth User current,
                                                        @RequestBody List<Long> tagIds) {
        assignmentQuizTagService.editAssignmentQuizTags(assignmentId, quizId, studentId, current.getId(), tagIds);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @PostMapping("programs/assignmentQuizTags")
    public ResponseEntity addAssignmentQuizTag(@Auth User current, @RequestBody AssignmentQuizTag assignmentQuizTag) {
        Map<String, Object> map = new HashMap<>();
        AssignmentQuizTag assignmentQuizTagInfo = assignmentQuizTagService.addAssignmentQuizTag(current, assignmentQuizTag);
        map.put("id", assignmentQuizTagInfo.getId());

        return new ResponseEntity(map, HttpStatus.CREATED);
    }

    @PutMapping("programs/assignmentQuizTags/{assignmentQuizTagId}")
    public ResponseEntity editAssignmentQuizTag(@PathVariable Long assignmentQuizTagId, @RequestBody AssignmentQuizTag assignmentQuizTag) throws BusinessException {
        assignmentQuizTagService.editAssignmentQuizTag(assignmentQuizTagId, assignmentQuizTag);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping("programs/assignmentQuizTags/{assignmentQuizTagId}")
    public ResponseEntity deleteAssignmentQuizTag(@PathVariable Long assignmentQuizTagId) {
        assignmentQuizTagService.deleteAssignmentQuizTags(assignmentQuizTagId);

        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}